const productMaterialTypeEnum = {
    PART:{value:1,name:"零件"},
    SEMI_FINISHED_PRODUCT:{value:2,name:"半成品"},
    FINISHED_PRODUCT:{value:3,name:"成品"},
}

$.extend({
    productMaterialTypeBusiness:{//产品次啊了类型业务对象
        prefix:ctx +'central/centralProductMaterialType',
        materialType:{
            //成品
            FINISHED_PRODUCT:3,
            //半成品
            SEMI_FINISHED_PRODUCT:2,
            //零件
            PART_PRODUCT:1
        },
        getSimpleList(selectOptions,params,callback){
            $.operation.buildSimpleSelect(selectOptions,{prefix:this.prefix,params:params},callback)
        }
    },areaBusiness:{//区域业务对象
        prefix:ctx+'central/centralArea',
        getSimpleList(selectOptions,params,callback){
            $.operation.buildSimpleSelect(selectOptions,{prefix:this.prefix,params:params},callback)
        }
    },productTypeBusiness:{//产品类型业务对象
        prefix:ctx+"central/centralProductType",
        getParentMaterialTypeList(selectOptions,materialTypeId,callback){//根据物料编号获取父物料集合
            console.log("===getParentMaterialTypeList===")
            $.operation.buildSimpleSelect(selectOptions,{url:`${this.prefix}/getParentMaterialTypeList/${materialTypeId}`},callback)
        },
        fillParamsOfProductMaterialTypeId(params,productMaterialType){
            if(!params ){
                params = {}
            }
            params["productMaterialTypeId"] = productMaterialType.value
            return params
        },selectedOptionsConvertToObj(selectedOptions){
            if(selectedOptions){
                const elementName = selectedOptions
                selectedOptions = {$select:$(elementName)}
            }
            return selectedOptions
        },
        //获取物料的简单列表
        getSimpleListOfPart(selectOptions,params,callback){
            params = this.fillParamsOfProductMaterialTypeId(params,productMaterialTypeEnum.PART)
            this.getSimpleList(selectOptions,params,callback)
        },
        //获取产品的简单列表
        getSimpleListOfFinishedProduct(selectOptions,params,callback){
            params = this.fillParamsOfProductMaterialTypeId(params,productMaterialTypeEnum.FINISHED_PRODUCT)
            this.getSimpleList(selectOptions,params,callback)
        },
        //获取所有的简单列表
        getSimpleList(selectOptions,params,callback){
            $.operation.buildSimpleSelect(selectOptions,{prefix:this.prefix,params:params},callback)
        },getSyntheticProductSimpleList(selectOptions,params,callback){//获取合成产品简单列表
            const materialType = $.productMaterialTypeBusiness.materialType
            const productMaterialTypes = [materialType.FINISHED_PRODUCT,materialType.SEMI_FINISHED_PRODUCT]
            this.getProductSimpleListByProductTypes(productMaterialTypes,selectOptions,params,callback)
        },getSemiFinishedProductSimpleList(selectOptions,params,callback){//获取半成品简单列表
            const materialType = $.productMaterialTypeBusiness.materialType
            const productMaterialTypes = [materialType.SEMI_FINISHED_PRODUCT]
            this.getProductSimpleListByProductTypes(productMaterialTypes,selectOptions,params,callback)
        },getPartProductSimpleList(selectOptions,params,callback){//获取零件产品列表
            const materialType = $.productMaterialTypeBusiness.materialType
            const productMaterialTypes = [materialType.PART_PRODUCT,materialType.SEMI_FINISHED_PRODUCT]
            this.getProductSimpleListByProductTypes(productMaterialTypes,selectOptions,params,callback)
        },getProductSimpleListByProductTypes(productMaterialTypes = [],selectOptions,params,callback){
            const materialType = $.productMaterialTypeBusiness.materialType
            selectOptions = this.selectedOptionsConvertToObj(selectOptions)
            selectOptions.filter = (data)=>{
                return data.filter(item=>productMaterialTypes.includes(item.data.productMaterialTypeId) )
            }
            this.getSimpleList(selectOptions,{prefix:this.prefix,params:params},callback)
        }
    },operationStationBusiness:{//操作工位业务对象
        prefix:ctx+"central/centralOperationStation",
        getSimpleList(selectOptions,params,callback){
            $.operation.buildSimpleSelect(selectOptions,{prefix:this.prefix,params:params},callback)
        }
    },materialBusiness:{//物料管理相关业务
        prefix:ctx+"central/centralMaterial",
        getSimpleList(selectOptions,params,callback){
            $.operation.buildSimpleSelect(selectOptions,{prefix:this.prefix,params:params},callback)
        },//获取唯一的产品批次号
        getDistinctMaterialBatchNoList(selectOptions,params,callback){

            this.getSimpleList(null,params,(res)=>{
                let data = res.data
                if(data && Array.isArray(data)){

                    const batchNoList = Array.from(new Set(data.map(item=>item.label)))

                    const batchNoObjList = []

                    batchNoList.forEach((item)=>batchNoObjList.push({value:item,label:item}))

                    selectOptions.data = batchNoObjList

                    selectOptions.fillDefault = false

                    $.selectComponent.init(selectOptions)

                }
                if(callback instanceof Function){
                    callback(res)
                }
            })
        }
    }
})